<template>
  <div>
    <basic-container>
      推荐位管理 / {{ $route.query.name }} / 书籍列表
      <el-button
        size="mini"
        @click="
          () => {
            $router.go(-1);
          }
        "
        >返回</el-button
      >
    </basic-container>
    <basic-container>
      <avue-crud
        ref="crud"
        :data="data"
        :option="option"
        v-model="form"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
        @row-update="rowUpdate"
      >
        <template slot="last" slot-scope="scope">
          {{ scope.row.edit_time }}
          <el-tag>{{ scope.row.edit_admin_name }}</el-tag>
        </template>
        <template slot="menuRight">
          <el-tag>数据共{{ data.length }}条</el-tag>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { common } from "@/api/cat/httpRequest";
import { catUrl } from "@/config/env";
import {
  appConfigBookRecommend as baseURL,
  bookCategory,
} from "@/api/cat/baseUrls";
export default {
  data() {
    return {
      page: {
        total: 0,
        pageSize: 10,
      },
      type: true,
      data: [],
      totalData: [],
      params: {},
      form: {},
      option: {
        addBtn: false,
        editBtn: true,
        delBtn: false,
        index: true,
        indexLabel: "编号",
        column: [
          {
            label: "分类",
            prop: "category_id_1",
            type: "select",
            search: false,
            dicUrl: `${catUrl + bookCategory.getListByLeave}/1`,
            dicFormatter: (res) => {
              return res.source;
            },
            props: {
              label: "label",
              value: "value",
            },
            addDisplay: false,
            editDisplay: false,
            addDisabled: false,
            editDisabled: false,
          },
          {
            label: "书籍id",
            prop: "book_id",
            type: "input",
            search: true,
            placeholder: "书籍id",
            rules: [
              {
                required: true,
                message: "请输入书籍id",
              },
            ],
          },
          {
            label: "书籍名称",
            prop: "book_title",
            type: "input",
            search: false,
            readonly: true,
            placeholder: "书籍名称",
            addDisplay: false,
            editDisplay: false,
            addDisabled: false,
            editDisabled: false,
            readonly: true,
          },
          {
            label: "排序",
            prop: "sort",
            type: "input",
            search: false,
            placeholder: "排序",
            rules: [
              {
                required: true,
                message: "请输入排序",
              },
            ],
          },
          {
            label: "最后操作信息",
            prop: "last",
            type: "input",
            addDisplay: false,
            editDisplay: false,
            addDisabled: false,
            editDisabled: false,
            readonly: true,
            slot: true,
          },
        ],
      },
    };
  },
  methods: {
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        `${baseURL.getBookInfoList}/${this.$route.query.id}`,
        this.params,
        "GET"
      );
      response.data.forEach((e, index) => (e.sort = index + 1));
      this.data = response.data;
      this.totalData = response.data;
      this.loading = false;
    },
    /**修改 */
    async rowUpdate(row, index, done, loading) {
      console.log(row);
      this.totalData.forEach((e) => {
        if (e.book_title == row.book_title) {
          e.sort = row.sort;
          e.book_id = row.book_id;
        }
      });
      this.totalData.sort((d1, d2) => d1.sort - d2.sort);
      let userInfo = this.$store.state.user.userInfo;
      let catRecommend = {
        id: this.$route.query.id,
        book_ids: this.totalData.map((e) => e.book_id),
        edit_admin_id: userInfo.id ? userInfo.id : null,
        edit_admin_name: userInfo.username,
      };
      let data = await common(
        `${baseURL.updateTwo}`,
        catRecommend,
        null,
        "POST"
      );
      if (data == null) {
        this.getList();
        loading();
        return;
      }
      this.getList();
      done();
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    /**搜索 */
    searchChange(params, done) {
      if (params.book_id) {
        this.data = this.totalData.filter(
          (e) => (e.book_id + "").indexOf(params.book_id) != -1
        );
      } else {
        this.data = this.totalData;
      }
      if (done) done();
    },
  },
};
</script>

<style>
</style>